package com.emm.yixun.mobile.ui.from;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.emm.yixun.mobile.R;
import com.emm.yixun.mobile.adapter.AnnexAdapter;
import com.emm.yixun.mobile.adapter.NewFromAdapter;
import com.emm.yixun.mobile.adapter.PhoneSelectAdapter;
import com.emm.yixun.mobile.application.EmmApplication;
import com.emm.yixun.mobile.base.BaseActivity;
import com.emm.yixun.mobile.model.AddpirModel;
import com.emm.yixun.mobile.model.BusinessAudit;
import com.emm.yixun.mobile.model.GetPayDepositDetail;
import com.emm.yixun.mobile.model.GetRoomList.RoomList;
import com.emm.yixun.mobile.model.NewClassKehuModel;
import com.emm.yixun.mobile.ui.RejectActivity;
import com.emm.yixun.mobile.ui.signed.NewSignedActivity;
import com.emm.yixun.mobile.ui.subscribe.NewSubscribeActivity;
import com.eroad.product.tools.MyGridView;
import com.eroad.product.tools.MyListView;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.yixun.app.viewpagerandimag.sample.ViewPagerActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import me.imid.swipebacklayout.lib.SwipeBackLayout;
import me.imid.swipebacklayout.lib.app.SwipeBackActivity;
import xf.tools.Loading;

/**
 * <p>
 * <b>Title：</b> 认筹详情
 * </p>
 * <p>
 * <b>Description：</b>
 * </p>
 *
 * @author tangxin
 * @date 2016年12月6日 下午4:09:06
 */
public class FromDetailsActivity extends SwipeBackActivity {

    private static final String TAG = "FromListDetailsActivity";

    /**
     * 认筹ID
     */
    private String payDepositId;

    /**
     * 申请驳回
     */
    TextView reject_btn;

    /**
     * 审核通过
     */
    TextView is_sure;

    /**
     * 标题
     */
    TextView title_main;

    /**
     * 返回按钮
     */
    ImageView back_btn;

    ArrayList<NewClassKehuModel> list = new ArrayList<NewClassKehuModel>();

    NewFromAdapter adapter;

    MyListView kehuxinxi;

    /**
     * 名称
     */
    TextView user_name;

    /**
     * 标星
     */
    TextView user_star;

    /**
     * 性别
     */
    TextView user_sex;

    /**
     * 评分
     */
    TextView user_number;

    TextView customerStatus_view;

    TextView customerOrigin_view;

    /**
     * 认筹房源
     */
    TextView payDepositHouses_view;

    /**
     * 房源标识
     */
    LinearLayout found_1, found_2;

    /**
     * 是否无房认筹
     */
    TextView set_is_no;

    /**
     * 计价面积
     */
    TextView calculateArea_view;

    /**
     * 折前单价
     */
    TextView discountPrice_view;

    /**
     * 折前总价
     */
    TextView discountTotalPrice_view;

    /**
     * 客户名称
     */
    TextView memberName_1_view;

    /**
     * 证件类型
     */
    TextView documentType_1_view;

    /**
     * 证件号码
     */
    TextView documentNumber_1_view;

    /**
     * 手机号
     */
    TextView mobile_view;

    /**
     * 邮寄地址
     */
    TextView mailPost_view;

    /**
     * 邮编
     */
    TextView postcode_view;

    /**
     * 认筹编号
     */
    TextView payDepositCode_view;

    /**
     * 认筹金
     */
    TextView payDepositMoney_view;

    /**
     * 经纪人
     */
    TextView agentId_view;

    /**
     * 认筹时间
     */
    TextView payDepositDate_view;

    /**
     * 备注
     */
    TextView remark_view;

    /**
     * 竖线
     */
    TextView v_lins;

    /**
     * 1待提交 2待审核 3驳回 4成功
     */
    private int opentype = 0;

    LinearLayout buttom_select;

    /**
     * 手机号码
     */
    TextView phone_tel;

    /**
     * 审核状态
     */
    TextView auditStatus_view;

    /**
     * 理由
     */
    TextView auditReason;

    /**
     * 审核人
     */
    TextView auditId_view;

    /**
     * 审核时间
     */
    TextView auditTime_view;

    /**
     * 是否从审核列表过来 1 是，0否
     */
    private String IsToExamine = "0";

    /**
     * 房源信息
     */
    private boolean IsFangyuan = true;

    /**
     * 客户信息
     */
    private boolean IsKehu = true;
    /**
     * 客户信息
     */
    LinearLayout kehu, kehu_layout;

    LinearLayout fangyuan, fangyuan_layout, yewu_type, yewu_linear;

    private boolean IsYEWU = true;

    ImageView rt1, rt2, rt0, rt3;

    /**
     * 客户ID
     */
    private String customerId;

    /**
     * 认筹状态
     */
    TextView yewu_view;

    private String taskId = "";

    LinearLayout callphone_5;

    /**
     * 侧滑关闭框架
     */
    private SwipeBackLayout mSwipeBackLayout;

    private TextView realAmount_view;

    /**
     * 扫面列表grid
     */
    MyGridView grid_phone;

    /**
     * 合同扫面适配器
     */
    PhoneSelectAdapter padapter;

    ArrayList<AddpirModel> ap;

    /**
     * 图片附件
     */
    TextView from_htsm_view;

    /**
     * 合同扫面
     */
    private boolean IShtsm = true;

    /**
     * 合同扫面件
     */
    LinearLayout phone;

    /**
     * 扫面件
     */
    RelativeLayout phone_layout;

    ImageView rt1_p;

    /**
     * 附件布局
     */
    LinearLayout annex_layout;

    /**
     * 附件列表
     */
    MyListView annexList_list;

    /**
     * 附件数量
     */
    TextView annexNum_view;

    /**
     * 审核信息
     */
    private boolean IsAuditing = true;

    /**
     * 审核信息title
     */
    LinearLayout auditing_view;

    /**
     * 审核信息内容
     */
    LinearLayout auditing_c_layout;

    /**
     * 图片附件数量
     */
    TextView image_num;

    /**
     * 无附件显示
     */
    LinearLayout image_layout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fromlist_details_activity);

        SetSwipeBackLayout(mSwipeBackLayout);

        BaseActivity.activity = this;

        payDepositId = getIntent().getStringExtra("payDepositId");

        IsToExamine = getIntent().getStringExtra("IsToExamine");

        customerId = getIntent().getStringExtra("customerId");

        taskId = getIntent().getStringExtra("taskId");

        initView();

        SetRelativiLayoutPadding(R.id.main_head_top_rela);

        initBtn();

        getPayDepositDetail();
    }

    /**
     * <p>
     * <b>Title：</b> 初始化view
     * </p>
     * <p>
     * <b>Description：</b>
     * </p>
     *
     * @author tangxin
     * @date 2016年12月7日 下午3:05:52
     */
    private void initView() {
        fangyuan = (LinearLayout) findViewById(R.id.fangyuan);
        fangyuan_layout = (LinearLayout) findViewById(R.id.fangyuan_layout);
        rt1 = (ImageView) findViewById(R.id.rt1);
        kehu = (LinearLayout) findViewById(R.id.kehu);
        kehu_layout = (LinearLayout) findViewById(R.id.kehu_layout);
        rt2 = (ImageView) findViewById(R.id.rt2);
        reject_btn = (TextView) findViewById(R.id.reject_btn);
        title_main = (TextView) findViewById(R.id.title_main);
        title_main.setText(getString(R.string.from_detailsd));
        back_btn = (ImageView) findViewById(R.id.back_btn);
        is_sure = (TextView) findViewById(R.id.is_sure);
        kehuxinxi = (MyListView) findViewById(R.id.kehuxinxi);
        user_name = (TextView) findViewById(R.id.user_name);
        user_star = (TextView) findViewById(R.id.user_star);
        user_sex = (TextView) findViewById(R.id.user_sex);
        user_number = (TextView) findViewById(R.id.user_number);
        customerStatus_view = (TextView) findViewById(R.id.customerStatus_view);
        customerOrigin_view = (TextView) findViewById(R.id.customerOrigin_view);
        payDepositHouses_view = (TextView) findViewById(R.id.payDepositHouses_view);
        calculateArea_view = (TextView) findViewById(R.id.calculateArea_view);
        discountPrice_view = (TextView) findViewById(R.id.discountPrice_view);
        discountTotalPrice_view = (TextView) findViewById(R.id.discountTotalPrice_view);
        memberName_1_view = (TextView) findViewById(R.id.memberName_1_view);
        documentType_1_view = (TextView) findViewById(R.id.documentType_1_view);
        documentNumber_1_view = (TextView) findViewById(R.id.documentNumber_1_view);
        mobile_view = (TextView) findViewById(R.id.mobile_view);
        mailPost_view = (TextView) findViewById(R.id.mailPost_view);
        postcode_view = (TextView) findViewById(R.id.postcode_view);
        payDepositCode_view = (TextView) findViewById(R.id.payDepositCode_view);
        payDepositMoney_view = (TextView) findViewById(R.id.payDepositMoney_view);
        agentId_view = (TextView) findViewById(R.id.agentId_view);
        payDepositDate_view = (TextView) findViewById(R.id.payDepositDate_view);
        remark_view = (TextView) findViewById(R.id.remark_view);
        v_lins = (TextView) findViewById(R.id.v_lins);
        buttom_select = (LinearLayout) findViewById(R.id.buttom_select);
        phone_tel = (TextView) findViewById(R.id.phone_tel);
        auditReason = (TextView) findViewById(R.id.auditReason_view);
        auditTime_view = (TextView) findViewById(R.id.auditTime_view);
        auditStatus_view = (TextView) findViewById(R.id.auditStatus_view);
        auditId_view = (TextView) findViewById(R.id.auditId_view);
        found_1 = (LinearLayout) findViewById(R.id.found_1);
        found_2 = (LinearLayout) findViewById(R.id.found_2);
        set_is_no = (TextView) findViewById(R.id.set_is_no);
        yewu_linear = (LinearLayout) findViewById(R.id.yewu_linear);
        yewu_type = (LinearLayout) findViewById(R.id.yewu_type);
        rt0 = (ImageView) findViewById(R.id.rt0);
        yewu_view = (TextView) findViewById(R.id.yewu_view);
        callphone_5 = (LinearLayout) findViewById(R.id.callphone_5);
        realAmount_view = (TextView) findViewById(R.id.realAmount_view);

        callphone_5.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (phone_tel.getText().toString() == null || "".equals(phone_tel.getText().toString())
                        || TextUtils.isEmpty(phone_tel.getText().toString())) {
                    return;
                }
                EmmApplication.callPhone(phone_tel.getText().toString(), customerId);
            }
        });

        phone = (LinearLayout) findViewById(R.id.phone);
        phone_layout = (RelativeLayout) findViewById(R.id.phone_layout);
        from_htsm_view = (TextView) findViewById(R.id.from_htsm_view);
        from_htsm_view.setText("图片附件");
        grid_phone = (MyGridView) findViewById(R.id.grid_phone);
        rt1_p = (ImageView) findViewById(R.id.rt1_p);
        annex_layout = (LinearLayout) findViewById(R.id.annex_layout);
        annexList_list = (MyListView) findViewById(R.id.annexList_list);
        annexNum_view = (TextView) findViewById(R.id.annexNum_view);
        auditing_view = (LinearLayout) findViewById(R.id.auditing_view);
        rt3 = (ImageView) findViewById(R.id.rt3);
        auditing_c_layout = (LinearLayout) findViewById(R.id.auditing_c_layout);
        image_num = (TextView) findViewById(R.id.image_num);
        image_layout = (LinearLayout) findViewById(R.id.image_layout);
    }

    /**
     * <p>
     * <b>Title：</b> 初始化监听事件
     * </p>
     * <p>
     * <b>Description：</b>
     * </p>
     *
     * @author tangxin
     * @date 2016年12月7日 下午3:21:38
     */
    private void initBtn() {
        IsOpenOrClose(fangyuan_layout, IsFangyuan, rt1);
        IsOpenOrClose(yewu_linear, IsYEWU, rt0);
        IsOpenOrClose(kehu_layout, IsKehu, rt2);
        IsOpenOrClose(phone_layout, IShtsm, rt1_p);
        IsOpenOrClose(auditing_c_layout, IsAuditing, rt3);
        auditing_view.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                DoAuditing(IsAuditing);
            }
        });

        yewu_type.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                DoKehuYeWu(IsYEWU);
            }
        });

        phone.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                DoHTSMOpenOrClose(IShtsm);
            }
        });

        reject_btn.setOnClickListener(new OnClickListener() {// 签约
            @Override
            public void onClick(View v) {
                // 1待提交 2待审核 3驳回 4成功
                if (opentype == 1 || opentype == 3) {
                    EmmApplication.T("敬请期待");
                } else if (opentype == 4) {
                    // 签约
                    if ("0".equals(payDepositRoomType)) {
                        EmmApplication.Ts(payDepositRoomeMsg);
                    } else {

                        if ("0".equals(getpaydepositdetail.getEnough())) {
                            EmmApplication.Ts("还未收完认筹金");
                            return;
                        }

                        Intent intent = new Intent();
                        intent.putExtra("customerId", customerId);

                        if ("1".equals(getpaydepositdetail.getIsPayDeposit())) {
                            intent.putExtra("OpenType", "1");
                        } else {
                            intent.putExtra("OpenType", "5");

                            EmmApplication.IsGet = true;
                            if (EmmApplication.IsGet) {
                                EmmApplication.housinglist.clear();
                            }

//									try {
//										getpaydepositdetail.setDiscountRate(EmmApplication.drop2(""
//												+ (Double.valueOf(getpaydepositdetail.getDiscountRate())).doubleValue()
//												* 100));
//									} catch (NullPointerException e) {
//										getpaydepositdetail.setDiscountRate("0");
//									}

                            RoomList b = new RoomList();
                            b.setRoomId(getpaydepositdetail.getRoomId());
                            Log.v(TAG, "roomid:" + getpaydepositdetail.getRoomId());
                            b.setRoomName(payDepositHouses_view.getText().toString().trim());// +

                            b.setDiscountAmount(getpaydepositdetail.getDiscountAmount());
                            b.setDiscountRate(EmmApplication.isNull(getpaydepositdetail.getDiscountRate()) ? EmmApplication.drop2(""
                                    + (Double.valueOf(getpaydepositdetail.getDiscountRate())).doubleValue() * 100) : "0");
                            b.setFloorType(getpaydepositdetail.getFloorType());
                            EmmApplication.housinglist.add(b);

                            RoomList r = new RoomList();
                            r.setCalculateArea(calculateArea_view.getText().toString().trim());
                            r.setDiscountAmount(getpaydepositdetail.getDiscountAmount());
                            r.setDiscountPrice(discountPrice_view.getText().toString().trim());
                            r.setDiscountRate(EmmApplication.isNull(getpaydepositdetail.getDiscountRate()) ? EmmApplication.drop2(""
                                    + (Double.valueOf(getpaydepositdetail.getDiscountRate())).doubleValue() * 100) : "0");
                            r.setDiscountTotalPrice(discountTotalPrice_view.getText().toString().trim());
                            r.setRoomId(getpaydepositdetail.getRoomId());
                            r.setRoomName(payDepositHouses_view.getText().toString().trim());
                            EmmApplication.room = r;
                        }

                        intent.putExtra("payDepositId", payDepositId);
                        intent.putExtra("payDepositCode", getpaydepositdetail.getPayDepositCode());
                        intent.putExtra("payDepositMoney", getpaydepositdetail.getPayDepositMoney());
                        intent.putExtra("realAmount", getpaydepositdetail.getRealAmount());
                        intent.putExtra("activitytype", "rc");
                        intent.setClass(FromDetailsActivity.this, NewSignedActivity.class);
                        startActivity(intent);
                    }

                }
                if ("1".equals(IsToExamine)) {// 从审核列表过来
                    Intent intent = new Intent();
                    intent.setClass(FromDetailsActivity.this, RejectActivity.class);
                    intent.putExtra("businessType", "1");
                    intent.putExtra("taskId", taskId);
                    intent.putExtra("payDepositId", payDepositId);
                    startActivity(intent);
                }
            }
        });

        back_btn.setVisibility(View.VISIBLE);
        back_btn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Loading.hideDialogForLoading();
                finish();
                if (EmmApplication.IsReload != 1) {
                    EmmApplication.IsReload = 1;
                    sendBroadcast(new Intent(EmmApplication.NEW_ON_CREATE_FROM_LIST));
                }
                if ("1".equals(IsToExamine)) {// 从审核列表过来
                    EmmApplication.IsReloadToExamine = true;
                }
            }
        });

        is_sure.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // 1待提交 2待审核 3驳回 4成功
                if (opentype == 1 || opentype == 3) {
                    EmmApplication.getpaydepositdetail = getpaydepositdetail;
                    Intent intent = new Intent();
                    intent.putExtra("customerId", customerId);
                    intent.putExtra("OpenType", 1);
                    intent.putExtra("payDepositId", payDepositId);
                    intent.setClass(FromDetailsActivity.this, NewFromActivity.class);
                    startActivity(intent);
                } else if (opentype == 4) {
                    // 认购
                    if ("0".equals(payDepositRoomType)) {
                        EmmApplication.Ts(payDepositRoomeMsg);
                    } else {

                        if ("0".equals(getpaydepositdetail.getEnough())) {
                            EmmApplication.Ts("还未收完认筹金");
                            return;
                        }

                        Intent intent = new Intent();
                        intent.putExtra("customerId", customerId);

                        if ("1".equals(getpaydepositdetail.getIsPayDeposit())) {
                            intent.putExtra("OpenType", "1");
                        } else {
                            intent.putExtra("OpenType", "5");

                            EmmApplication.IsGet = true;
                            if (EmmApplication.IsGet) {
                                EmmApplication.housinglist.clear();
                            }

//							try {
//								getpaydepositdetail.setDiscountRate(EmmApplication.drop2(""
//										+ (Double.valueOf(getpaydepositdetail.getDiscountRate())).doubleValue() * 100));
//							} catch (NullPointerException e) {
//								getpaydepositdetail.setDiscountRate("0");
//							}

                            RoomList b = new RoomList();
                            b.setRoomId(getpaydepositdetail.getRoomId());
                            Log.v(TAG, "roomid:" + getpaydepositdetail.getRoomId());
                            b.setRoomName(payDepositHouses_view.getText().toString().trim());// +
                            b.setDiscountAmount(getpaydepositdetail.getDiscountAmount());
                            b.setDiscountRate(EmmApplication.isNull(getpaydepositdetail.getDiscountRate()) ? EmmApplication.drop2(""
                                    + (Double.valueOf(getpaydepositdetail.getDiscountRate())).doubleValue() * 100) : "0");
                            b.setFloorType(getpaydepositdetail.getFloorType());
                            EmmApplication.housinglist.add(b);

                            RoomList r = new RoomList();
                            r.setCalculateArea(calculateArea_view.getText().toString().trim());
                            r.setDiscountAmount(getpaydepositdetail.getDiscountAmount());
                            r.setDiscountPrice(discountPrice_view.getText().toString().trim());
                            r.setDiscountRate(EmmApplication.isNull(getpaydepositdetail.getDiscountRate()) ? EmmApplication.drop2(""
                                    + (Double.valueOf(getpaydepositdetail.getDiscountRate())).doubleValue() * 100) : "0");
                            r.setDiscountTotalPrice(discountTotalPrice_view.getText().toString().trim());
                            r.setRoomId(getpaydepositdetail.getRoomId());
                            r.setRoomName(payDepositHouses_view.getText().toString().trim());
                            EmmApplication.room = r;
                        }

                        intent.putExtra("payDepositId", payDepositId);
                        intent.putExtra("payDepositCode", getpaydepositdetail.getPayDepositCode());
                        intent.putExtra("payDepositMoney", getpaydepositdetail.getPayDepositMoney());
                        intent.putExtra("realAmount", getpaydepositdetail.getRealAmount());
                        intent.setClass(FromDetailsActivity.this, NewSubscribeActivity.class);
                        startActivity(intent);
                    }
                }
                if ("1".equals(IsToExamine)) {// 从审核列表过来
                    popu();
                }
            }
        });

        fangyuan.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                DoOpenOrClose(IsFangyuan);
            }
        });

        kehu.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                DoKehuOpenOrClose(IsKehu);
            }
        });

        padapter = new PhoneSelectAdapter(FromDetailsActivity.this, ap, 0);
        grid_phone.setAdapter(padapter);
        grid_phone.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if ("1".equals(ap.get(position).getType())) {
                    ViewPagerActivity.postion = position;
                    ViewPagerActivity.sDrawables.clear();
                    for (int u = 0; u < ap.size(); u++) {
                        if ("1".equals(ap.get(u).getType())) {
                            ViewPagerActivity.sDrawables.add(ap.get(u).getPath());
                        }
                    }
                    Intent intent = new Intent(FromDetailsActivity.this, ViewPagerActivity.class);
                    startActivity(intent);
                }
            }
        });
    }

    /**
     * <p>
     * <b>Title：</b> 判断操作中的审核信息模块的展开状况
     * </p>
     * <p>
     * <b>Description：</b>
     * </p>
     *
     * @param bl
     * @author tangxin
     * @date 2016年12月7日 下午4:53:17
     */
    private void DoAuditing(boolean bl) {
        if (!bl) {
            IsAuditing = true;
            auditing_c_layout.setVisibility(View.VISIBLE);
            rt3.setImageResource(R.drawable.j_bottom);
        } else {
            IsAuditing = false;
            auditing_c_layout.setVisibility(View.GONE);
            rt3.setImageResource(R.drawable.j_top);
        }
    }

    /**
     * <p>
     * <b>Title：</b> 判断操作中的客户信息模块的展开状况
     * </p>
     * <p>
     * <b>Description：</b>
     * </p>
     *
     * @param bl
     * @author tangxin
     * @date 2016年12月7日 下午3:24:01
     */
    private void DoKehuOpenOrClose(boolean bl) {
        if (!bl) {
            IsKehu = true;
            kehu_layout.setVisibility(View.VISIBLE);
            rt2.setImageResource(R.drawable.j_bottom);
        } else {
            IsKehu = false;
            kehu_layout.setVisibility(View.GONE);
            rt2.setImageResource(R.drawable.j_top);
        }
    }

    /**
     * <p>
     * <b>Title：</b> 判断操作中的客户信息模块的展开状况
     * </p>
     * <p>
     * <b>Description：</b>
     * </p>
     *
     * @param bl
     * @author tangxin
     * @date 2016年12月7日 下午3:24:29
     */
    private void DoKehuYeWu(boolean bl) {
        if (!bl) {
            IsYEWU = true;
            yewu_linear.setVisibility(View.VISIBLE);
            rt0.setImageResource(R.drawable.j_bottom);
        } else {
            IsYEWU = false;
            yewu_linear.setVisibility(View.GONE);
            rt0.setImageResource(R.drawable.j_top);
        }
    }

    /**
     * <p>
     * <b>Title：</b> 判断操作中的合同扫面件的展开状况
     * </p>
     * <p>
     * <b>Description：</b>
     * </p>
     *
     * @param bl
     * @author tangxin
     * @date 2016年12月7日 下午4:48:52
     */
    private void DoHTSMOpenOrClose(boolean bl) {
        if (!bl) {
            IShtsm = true;
            phone_layout.setVisibility(View.VISIBLE);
            rt1_p.setImageResource(R.drawable.j_bottom);
        } else {
            IShtsm = false;
            phone_layout.setVisibility(View.GONE);
            rt1_p.setImageResource(R.drawable.j_top);
        }
    }

    /**
     * <p>
     * <b>Title：</b> 判断各展开状况
     * </p>
     * <p>
     * <b>Description：</b>
     * </p>
     *
     * @param view
     * @param bl
     * @param view2
     * @author tangxin
     * @date 2016年12月7日 下午3:24:47
     */
    private void IsOpenOrClose(View view, boolean bl, ImageView view2) {
        if (bl) {
            view.setVisibility(View.VISIBLE);
            view2.setImageResource(R.drawable.j_bottom);
        } else {
            view.setVisibility(View.GONE);
            view2.setImageResource(R.drawable.j_top);
        }
    }

    /**
     * <p>
     * <b>Title：</b> 判断操作中的房源模块的展开状况
     * </p>
     * <p>
     * <b>Description：</b>
     * </p>
     *
     * @param bl
     * @author tangxin
     * @date 2016年12月7日 下午3:24:57
     */
    private void DoOpenOrClose(boolean bl) {
        if (!bl) {
            IsFangyuan = true;
            fangyuan_layout.setVisibility(View.VISIBLE);
            rt1.setImageResource(R.drawable.j_bottom);
        } else {
            IsFangyuan = false;
            fangyuan_layout.setVisibility(View.GONE);
            rt1.setImageResource(R.drawable.j_top);
        }
    }

    View view3;
    PopupWindow popu_call;

    /**
     * <p>
     * <b>Title：</b> 审核通过弹出框
     * </p>
     * <p>
     * <b>Description：</b>
     * </p>
     *
     * @author tangxin
     * @date 2016年12月7日 下午3:25:08
     */
    private void popu() {
        view3 = LayoutInflater.from(FromDetailsActivity.this).inflate(R.layout.is_sure_popu, null);
        popu_call = new PopupWindow(view3, android.support.v4.view.ViewPager.LayoutParams.FILL_PARENT,
                android.support.v4.view.ViewPager.LayoutParams.FILL_PARENT);
        popu_call.setFocusable(true);

        popu_call.setSoftInputMode(PopupWindow.INPUT_METHOD_NEEDED);
        popu_call.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

        popu_call.showAtLocation(view3, Gravity.CENTER, 0, 0);
        popu_call.showAsDropDown(view3, 0, 0);
        popu_call.setFocusable(true);
        popu_call.setOutsideTouchable(true);
        view3.setFocusable(true);
        view3.setFocusableInTouchMode(true);

        view3.setOnKeyListener(new OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK) {
                }
                return false;
            }
        });
        // 取消
        TextView dismiss = (TextView) view3.findViewById(R.id.dismiss);
        // 确定
        TextView shuerbtn = (TextView) view3.findViewById(R.id.shuerbtn);
        // 输入框
        final TextView content_ed = (TextView) view3.findViewById(R.id.content_ed);

        dismiss.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                popu_call.dismiss();
            }
        });
        shuerbtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // if (TextUtils.isEmpty(content_ed.getText().toString().trim())
                // || null == content_ed.getText().toString().trim()) {
                // EmmApplication.T("请输入内容");
                // } else {
                popu_call.dismiss();
                // 校验成功之后的操作
                businessAudit("认筹", "1", content_ed.getText().toString().trim());
                // }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        BaseActivity.activity = this;
        IntentFilter filter = new IntentFilter(EmmApplication.NEW_ON_CREATE_FROM);
        registerReceiver(mBroadcastReceiver, filter);
        IntentFilter filter2 = new IntentFilter(EmmApplication.FROM_LIST_DETAILS_FI_NI_ACTIVITY);
        registerReceiver(mBroadcastReceiver, filter2);
        if (EmmApplication.IsReload != 1) {
            Log.v(TAG, ":-------1");
        }
    }

    private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(EmmApplication.NEW_ON_CREATE_FROM)) {
                // 刷新动作
                Log.v(TAG, ":-------");
                getPayDepositDetail();
            }
            if (action.equals(EmmApplication.FROM_LIST_DETAILS_FI_NI_ACTIVITY)) {
                // 关闭界面
                Log.v(TAG, ":-------关闭界面");
                EmmApplication.IsReloadToExamine = true;
                finish();
            }
        }
    };

    protected void onDestroy() {
        super.onDestroy();
        EmmApplication.IsLoadingFromList = false;// 解除限制
        unregisterReceiver(mBroadcastReceiver);
    }

    ;

    /**
     * 查看认筹详情
     */
    GetPayDepositDetail getpaydepositdetail;

    /**
     * 认筹房源类型： 默认返回 0
     */
    private String payDepositRoomType = "999";

    /**
     * 认筹房源消息
     */
    private String payDepositRoomeMsg = "";

    /**
     * <p>
     * <b>Title：</b> 查看认筹详情接口
     * </p>
     * <p>
     * <b>Description：</b>
     * </p>
     *
     * @author tangxin
     * @date 2016年12月7日 下午3:25:45
     */
    private void getPayDepositDetail() {
        EmmApplication.updateUrl("getPayDepositDetail");
        Map<String, String> map = new HashMap<String, String>();
        map.put("version", EmmApplication.getPackageManager(5));
        map.put("merchantId", EmmApplication.getData(EmmApplication.merchantId));
        map.put("token", EmmApplication.getToken());
        map.put("userId", EmmApplication.getUserId());
        map.put("terminalCode", EmmApplication.getPackageManager(2));
        map.put("projectCode", EmmApplication.getProjectCode());
        map.put("payDepositId", payDepositId);
        map.put("isAudit", IsToExamine);

        Map<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("params", map);
        JSONObject json = new JSONObject(paramsMap);
        String jsonstr = json.toString();
        RequestParams params = new RequestParams();
        params.put("body", jsonstr);
        Log.v(TAG, "getPayDepositDetail-->post:" + EmmApplication.urlStr + "?body=" + jsonstr);
        AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
        asyncHttpClient.post(EmmApplication.urlStr, params, new AsyncHttpResponseHandler() {
            @Override
            public void onFailure(Throwable error, String content) {
                super.onFailure(error, content);
                Loading.hideDialogForLoading();
                Log.v(TAG, "请求结束error:" + error + "==" + content);
            }

            @Override
            public void onFinish() {
                Loading.hideDialogForLoading();
                Log.v(TAG, "请求结束");
                super.onFinish();
            }

            @Override
            public void onStart() {
                super.onStart();
                Loading.hideDialogForLoading();
                Loading.showDialogForLoading(FromDetailsActivity.this, "正在加载...", false, true, 60000);
                Log.v(TAG, "请求开始");
            }

            @Override
            public void onSuccess(String content) {
                super.onSuccess(content);
                if (content == null) {
                    return;
                }
                Log.v(TAG, content);
                JSONObject jsonObj = JSONObject.parseObject(content);
                JSONObject resData = (JSONObject) jsonObj.get("resData");
                if (resData == null) {
                    Log.v(TAG, "信息返回值为空");
                    return;
                }
                getpaydepositdetail = JSONObject.parseObject(resData.toString(), GetPayDepositDetail.class);

                if ("1".equals(getpaydepositdetail.getResult())) {
                    Log.v("获取成功", "");

                    realAmount_view.setText(getpaydepositdetail.getRealAmount());

                    try {
                        payDepositRoomType = getpaydepositdetail.getPayDepositRoomType();
                    } catch (NullPointerException e) {
                        Log.v(TAG, "payDepositRoomType-->为空");
                        payDepositRoomType = "999";
                    }

                    try {
                        payDepositRoomeMsg = getpaydepositdetail.getPayDepositRoomeMsg();
                    } catch (NullPointerException e) {
                        Log.v(TAG, "payDepositRoomeMsg-->为空");
                    }

                    yewu_view.setText(getpaydepositdetail.getBusinessStatus());

                    user_name.setText(getpaydepositdetail.getCustomerName());

                    user_star.setVisibility("是".equals(getpaydepositdetail.getCustomerStar()) == true ? View.VISIBLE
                            : View.INVISIBLE);

                    phone_tel.setText(getpaydepositdetail.getCustomerPhone());

                    user_sex.setText(getpaydepositdetail.getCustomerSex());

                    user_number.setText(getpaydepositdetail.getIntentionLevel());

                    customerStatus_view.setText(getpaydepositdetail.getCustomerStatus());
                    customerOrigin_view.setText(getpaydepositdetail.getCustomerOrigin());

                    if ("1".equals(getpaydepositdetail.getIsPayDeposit())) {
                        set_is_no.setText("是");
                        found_1.setVisibility(View.GONE);
                    } else {
                        set_is_no.setText("否");
                        found_1.setVisibility(View.VISIBLE);
                        payDepositHouses_view.setText(getpaydepositdetail.getPayDepositHouses());

                        calculateArea_view.setText(getpaydepositdetail.getCalculateArea());

                        discountPrice_view.setText(getpaydepositdetail.getDiscountPrice());

                        discountTotalPrice_view.setText(getpaydepositdetail.getDiscountTotalPrice());
                    }

                    memberName_1_view.setText(getpaydepositdetail.getCustomerName());

                    documentType_1_view.setText(getpaydepositdetail.getDocumentType());

                    documentNumber_1_view.setText(getpaydepositdetail.getDocumentNumber());

                    if (null != getpaydepositdetail.getCustomerFamilyList()
                            && getpaydepositdetail.getCustomerFamilyList().size() > 0) {

                        ArrayList<NewClassKehuModel> list = new ArrayList<NewClassKehuModel>();
                        for (int i = 0; i < getpaydepositdetail.getCustomerFamilyList().size(); i++) {
                            NewClassKehuModel m = new NewClassKehuModel();
                            m.setCode(getpaydepositdetail.getCustomerFamilyList().get(i).getDocumentNumber());
                            m.setName(getpaydepositdetail.getCustomerFamilyList().get(i).getMemberName());
                            m.setNumber("" + (i + 1));
                            list.add(m);
                        }
                        adapter = new NewFromAdapter(FromDetailsActivity.this, list);
                        kehuxinxi.setAdapter(adapter);
                        adapter.SetType(1);
                    }

                    mobile_view.setText(getpaydepositdetail.getMobile());

                    mailPost_view.setText(getpaydepositdetail.getMailPost());

                    postcode_view.setText(getpaydepositdetail.getPostcode());

                    payDepositCode_view.setText(getpaydepositdetail.getPayDepositCode());

                    payDepositMoney_view.setText(getpaydepositdetail.getPayDepositMoney());

                    agentId_view.setText(getpaydepositdetail.getAgentId());

                    payDepositDate_view.setText(getpaydepositdetail.getPayDepositDate());

                    remark_view.setText(EmmApplication.isNull(getpaydepositdetail.getRemark()) ? getpaydepositdetail
                            .getRemark() : "暂无备注");

                    auditStatus_view.setText(getpaydepositdetail.getAuditStatus());

                    auditReason.setText(getpaydepositdetail.getAuditReason());

                    auditId_view.setText(getpaydepositdetail.getAuditId());

                    auditTime_view.setText(getpaydepositdetail.getAuditTime());

                    if (null == getpaydepositdetail.getImageList() || getpaydepositdetail.getImageList().size() <= 0) {
                        image_num.setVisibility(View.GONE);
                        ArrayList<AddpirModel> listap = new ArrayList<AddpirModel>();
                        if (null == padapter) {
                            padapter = new PhoneSelectAdapter(FromDetailsActivity.this, listap, 0);
                            grid_phone.setAdapter(padapter);
                        } else {
                            padapter.SetDate(listap);
                        }
                    } else {
                        image_layout.setVisibility(View.GONE);
                        ap = new ArrayList<AddpirModel>();
                        for (int i = 0; i < getpaydepositdetail.getImageList().size(); i++) {
                            AddpirModel a = new AddpirModel();
                            a.setIsnet("2");// 是否网络图片
                            a.setType("1");// 图片或添加
                            a.setPath(getpaydepositdetail.getImageList().get(i).getSmallUrl());// 图片路径
                            ap.add(a);
                        }
                        image_num.setVisibility(View.VISIBLE);
                        image_num.setText(" (" + getpaydepositdetail.getImageList().size() + ")");
                        grid_phone.setNumColumns(1);
                        ArrayList<AddpirModel> listap = new ArrayList<AddpirModel>();
                        listap.add(ap.get(0));
                        if (null == padapter) {
                            padapter = new PhoneSelectAdapter(FromDetailsActivity.this, listap, 0);
                            grid_phone.setAdapter(padapter);
                        } else {
                            padapter.SetDate(listap);
                        }
                    }

                    if (null == getpaydepositdetail.getAnnexNum() || "0".equals(getpaydepositdetail.getAnnexNum())
                            || "null".equals(getpaydepositdetail.getAnnexNum())) {
                        annex_layout.setVisibility(View.GONE);
                        image_layout.setVisibility(View.GONE);
                        if (null == getpaydepositdetail.getImageList()
                                || getpaydepositdetail.getImageList().size() <= 0) {
                            // 显示没有附件图片
                            image_layout.setVisibility(View.VISIBLE);
                        }
                    } else {
                        image_layout.setVisibility(View.GONE);
                        annexNum_view.setText("存在以下" + getpaydepositdetail.getAnnexNum() + "个非图片附件，请通过电脑端查看");
                        annex_layout.setVisibility(View.VISIBLE);
                        AnnexAdapter annexAdapter = new AnnexAdapter(FromDetailsActivity.this, getpaydepositdetail
                                .getAnnexList());
                        annexList_list.setAdapter(annexAdapter);
                    }

                    if ("1".equals(IsToExamine)) {
                        buttom_select.setVisibility(View.VISIBLE);
                        is_sure.setVisibility(View.VISIBLE);
                        is_sure.setText("审核通过");
                        reject_btn.setVisibility(View.VISIBLE);
                        reject_btn.setText("审核退回");
                    } else {
                        // 待提交 待审核 驳回 成功
                        if ("待提交".equals(getpaydepositdetail.getAuditStatus())) {
                            buttom_select.setVisibility(View.VISIBLE);
                            v_lins.setVisibility(View.GONE);
                            reject_btn.setVisibility(View.GONE);
                            is_sure.setVisibility(View.VISIBLE);
                            is_sure.setText("认筹");
                            opentype = 1;
                        } else if ("审核中".equals(getpaydepositdetail.getAuditStatus())) {
                            opentype = 2;
                            buttom_select.setVisibility(View.GONE);
                        } else if ("已退回".equals(getpaydepositdetail.getAuditStatus())) {
                            opentype = 3;
                            buttom_select.setVisibility(View.VISIBLE);
                            v_lins.setVisibility(View.GONE);
                            reject_btn.setVisibility(View.GONE);
                            is_sure.setVisibility(View.VISIBLE);
                            is_sure.setText("认筹");
                        } else if ("已通过".equals(getpaydepositdetail.getAuditStatus())) {
                            opentype = 4;
                            buttom_select.setVisibility(View.VISIBLE);
                            v_lins.setVisibility(View.VISIBLE);
                            reject_btn.setVisibility(View.VISIBLE);
                            is_sure.setVisibility(View.VISIBLE);
                            is_sure.setText("认购");
                            reject_btn.setText("签约");
                        }
                    }
                } else {
                    EmmApplication.T(getpaydepositdetail.getErrorMsg().toString());
                    Log.v("获取失败", "errorCode:" + getpaydepositdetail.getErrorCode().toString() + "errorMsg:"
                            + getpaydepositdetail.getErrorMsg().toString());
                }
            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (EmmApplication.IsReload != 1) {
                EmmApplication.IsReload = 1;
                sendBroadcast(new Intent(EmmApplication.NEW_ON_CREATE_FROM_LIST));
            }
            if ("1".equals(IsToExamine)) {// 从审核列表过来
                EmmApplication.IsReloadToExamine = true;
            }
        } else {
        }
        return super.onKeyDown(keyCode, event);
    }

    BusinessAudit businessaudit;

    /**
     * <p>
     * <b>Title：</b> 查看认筹详情接口
     * </p>
     * <p>
     * <b>Description：</b>
     * </p>
     *
     * @param businessType 业务类型
     * @param auditStatus  审核状态
     * @param remark       备注
     * @author tangxin
     * @date 2016年12月7日 下午3:26:42
     */
    private void businessAudit(String businessType, String auditStatus, String remark) {
        EmmApplication.updateUrl("businessAudit");
        Map<String, String> map = new HashMap<String, String>();
        map.put("version", EmmApplication.getPackageManager(5));
        map.put("merchantId", EmmApplication.getData(EmmApplication.merchantId));
        map.put("token", EmmApplication.getToken());
        map.put("userId", EmmApplication.getUserId());
        map.put("terminalCode", EmmApplication.getPackageManager(2));
        map.put("projectCode", EmmApplication.getProjectCode());
        map.put("businessId", payDepositId);// 业务ID
        map.put("businessType", businessType);
        map.put("taskId", taskId);
        map.put("auditStatus", auditStatus);
        map.put("remark", remark);
        Map<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("params", map);
        JSONObject json = new JSONObject(paramsMap);
        String jsonstr = json.toString();
        RequestParams params = new RequestParams();
        params.put("body", jsonstr);
        Log.v("businessAudit-->post", EmmApplication.urlStr + "?body=" + jsonstr);
        AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
        asyncHttpClient.post(EmmApplication.urlStr, params, new AsyncHttpResponseHandler() {
            @Override
            public void onFailure(Throwable error, String content) {
                super.onFailure(error, content);
                Loading.hideDialogForLoading();
                Log.v(TAG, "请求结束error:" + error + "==" + content);
            }

            @Override
            public void onFinish() {
                Loading.hideDialogForLoading();
                Log.v(TAG, "请求结束");
                super.onFinish();
            }

            @Override
            public void onStart() {
                super.onStart();
                Loading.hideDialogForLoading();
                Loading.showDialogForLoading(FromDetailsActivity.this, "正在加载...", false, true, 60000);
                Log.v(TAG, "请求开始");
            }

            @Override
            public void onSuccess(String content) {
                super.onSuccess(content);
                if (content == null) {
                    return;
                }
                JSONObject jsonObj = JSONObject.parseObject(content);
                JSONObject resData = (JSONObject) jsonObj.get("resData");
                if (resData == null) {
                    Log.v(TAG, "信息返回值为空");
                    return;
                }
                businessaudit = JSONObject.parseObject(resData.toString(), BusinessAudit.class);
                if ("1".equals(businessaudit.getResult())) {
                    Log.v("获取成功", "");
                    EmmApplication.T("审核成功");
                    EmmApplication.IsReloadToExamine = true;
                    finish();
                } else {
                    EmmApplication.T(businessaudit.getErrorMsg().toString());
                    Log.v("获取失败", "errorCode:" + businessaudit.getErrorCode().toString() + "errorMsg:"
                            + businessaudit.getErrorMsg().toString());
                }
            }
        });
    }
}
